package main

import (
	"fmt"
	"log"
	"time"

	_ "gitee.com/general252/kingbase-xorm0"
	"github.com/go-xorm/xorm"
)

type Person struct {
	UID   string `xorm:"'uid' not null unique VARCHAR(64)"`
	Name  string
	Age   int
	Index int
}

type Label struct {
	Id   int64
	Name string `json:"name"`
}

func main() {

	const (
		host     = "192.168.88.88"
		port     = 54321
		user     = "system"
		password = "123456"
		dbname   = "test"
	)

	connInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname) // kingbase
	connInfo = fmt.Sprintf("kingbase://%v:%v@%v:%v/%v?sslmode=disable", user, password, host, port, dbname)
	// connInfo = fmt.Sprintf("%v:%v@%v:%v/%v?charset=utf8", user, password, host, port, dbname) // mysql

	engine, err := xorm.NewEngine("kingbase", connInfo)
	if err != nil {
		panic(err)
	}

	engine.ShowSQL(true)

	if err = engine.Ping(); err != nil {
		panic(err)
	}

	if err = engine.Sync2(new(Person), new(Label)); err != nil {
		panic(err)
	}

	fmt.Println("Connection test success!")

	var label = &Label{
		Name: time.Now().Format(time.RFC3339),
	}

	if count, err := engine.Insert(label); err != nil {
		log.Println(err)
	} else {
		log.Printf("%v %#v", count, label)
	}

	if _, err = engine.Insert(&Person{
		UID:   "aa",
		Name:  "bb",
		Age:   11,
		Index: 22,
	}); err != nil {
		panic(err)
	}

	var person Person
	ok, err := engine.Get(&person)
	log.Println(ok, err, person)

	_ = engine.Close()
}
